package com.example.weibo3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取导航控制对象
        NavController navController = Navigation.findNavController(this, R.id.fragment);

        // 获取BottomNavigationView组件
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_view);
        // 添加事件
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // 编写fragment切换逻辑
                if (item.getItemId() == R.id.tab1) {
                    // 切换到第一个菜单关联的fragment
                    navController.navigate(R.id.mainFragment);
                } else if (item.getItemId() == R.id.tab2) {
                    navController.navigate(R.id.videoFragment);
                } else if (item.getItemId() == R.id.tab3) {
                    navController.navigate(R.id.searchFragment);
                } else if (item.getItemId() == R.id.tab4) {
                    navController.navigate(R.id.msgFragment);
                } else if (item.getItemId() == R.id.tab5) {
                    navController.navigate(R.id.myFragment);
                }
                return true;
            }
        });
    }
}